package tang.common;

import lombok.Data;

import java.nio.ByteBuffer;

/**
 * @author Tang
 * @classname Package
 * @description [  ]
 * @date 2021/2/23 20:50
 */
@Data
public class Packet {

    /**
     * 数据包的大小
     */
    public int len;

    public Record request;

    public Record response;

    public boolean finished;

    private ByteBuffer dataBuffer = ByteBuffer.allocate(64 * 1024);

    public OpCode type;

    public void createData() {
        request.encoder();
        // 数据包len + 类型 + 实际数据
        dataBuffer.put(ByteBuffer.allocate(4));
        dataBuffer.putInt(type.getCode());
        dataBuffer.put(request.getData());
        dataBuffer.flip().rewind();
        dataBuffer.putInt(request.getData().limit());
        dataBuffer.rewind();
    }

    public void readData(ByteBuffer respData) {
        response.decoder(respData);
    }
}